//存放单元格条件的MAP
var celltiaojianMap = new Map();

function addRowCellTiaojian(tableId){
	var cellXinxijiValue = $("#dataSetSelectCell").find("option:selected").text();
	var cellXinxijiId = $("#dataSetSelectCell").find("option:selected").val();
	var cellXinxixiangValue = $("#dataItemSelectCell").find("option:selected").text();
	var cellXinxixiangId = $("#dataItemSelectCell").find("option:selected").val();
	var cellLuojiyusuanfuValue = $("#cellLogicOperationType").find("option:selected").text();
	var cellLuojiyusuanfuId = $("#cellLogicOperationType").find("option:selected").val();
	var cellValue = $("#cellValue").val();
	var cellShifouyouxianValue = $("#cellBracketsType").find("option:selected").text();
	var cellShifouyouxianId = $("#cellBracketsType").find("option:selected").val();
	var cellLianjieValue = $("#cellConnectorType").find("option:selected").text();
	var cellLianjieId = $("#cellConnectorType").find("option:selected").val();
	var cellHangNum = $("#cellHangNum").val();
	var cellLieNum = $("#cellLieNum").val();
	if(cellXinxijiId == "0"){
		alert("请选择统计信息集");
		return false;
	}
	if(cellXinxixiangId == "0"){
		alert("请选择统计信息项");
		return false;
	}
	if(cellValue == null || cellValue == ''){
		alert("请填写单元格条件的值");
		return false;
	}
	//生成1-32位随机串
	var cellMapKey = randomString(true, 1, 32);
	var tableTr = handleDataCell(cellHangNum,cellLieNum,cellXinxijiValue,cellXinxixiangValue,cellLuojiyusuanfuValue,cellShifouyouxianValue,cellValue,cellLianjieValue,cellMapKey);
	$("#"+tableId).append(tableTr);
	var cellShiTi = new CellShiTi(cellHangNum,cellLieNum,cellXinxijiId,cellXinxixiangId,cellLuojiyusuanfuId,cellShifouyouxianId,cellValue,cellLianjieId);
	celltiaojianMap.put(cellMapKey,cellShiTi);
	$("#cellBracketsType option[value='0']").attr("selected","selected");
	$("#cellConnectorType option[value='0']").attr("selected","selected");
}
//增加单元格的汇总条件
function handleDataCell(hangNum,lieNum,cellXinxijiValue,cellXinxixiangValue,cellLuojiyusuanfuValue,cellShifouyouxianValue,cellValue,cellLianjieValue,cellTiaojianKey){
	var tr = '<tr id="' + cellTiaojianKey + 'Tr">';
    tr += '<td align="center" valign="middle" class="td_grey2">第' + hangNum + '行</td>';  
    tr += '<td align="center" valign="middle" class="td_grey2">第' + lieNum + '列</td>';  
    tr += '<td align="center" valign="middle" class="td_grey2">' + cellXinxijiValue + '</td>';  
    tr += '<td align="center" valign="middle" class="td_grey2">' + cellXinxixiangValue + '</td>';  
    tr += '<td align="center" valign="middle" class="td_grey2">' + cellLuojiyusuanfuValue + '</td>';  
    tr += '<td align="center" valign="middle" class="td_grey2">' + cellValue + '</td>';
    tr += '<td align="center" valign="middle" class="td_grey2">' + cellShifouyouxianValue + '</td>';
    tr += '<td align="center" valign="middle" class="td_grey2">' + cellLianjieValue + '</td>';
    tr += '<td align="center" valign="middle" class="td_grey2" id="' + cellTiaojianKey + '"><a href="#" onclick="deleteCelltiaojianTableTr(this)">删除</a>';
    tr += '||';
    tr += '<a href="#" onclick="tableTrUp(this)">上移</a>';
    tr += '||';
    tr += '<a href="#" onclick="tableTrDown(this)">下移</a></td>';
    tr += '<td style="display:none">' + cellTiaojianKey + '</td>';
    tr += '</tr>';
    return tr; 
}

//参数分别为“信息集ID”、“信息项ID”、“逻辑预算符ID”、“是否优先查询”、“总控条件下的值”、“连接符”
function CellShiTi(hangNum,lieNum,cellXinxijiId,cellXinxixiangId,cellLuojiyusuanfuId,cellShifouyouxianId,cellValue,cellLianjieId,orderId){
	this.hangNum=hangNum;
	this.lieNum=lieNum;
	this.cellXinxijiId=cellXinxijiId;
	this.cellXinxixiangId=cellXinxixiangId;
	this.cellLuojiyusuanfuId=cellLuojiyusuanfuId;
	this.cellShifouyouxianId=cellShifouyouxianId;
	this.cellValue=cellValue;
	this.cellLianjieId=cellLianjieId;
	this.orderId=orderId;
}

function cellSubNum(){
	var oldValue=parseInt($("#cellHangNum").val()); //取出现在的值，并使用parseInt转为int类型数据
	if(oldValue != 0){
		oldValue--;  //自减1
		$("#cellHangNum").val(oldValue);
	}
}
function cellAddNum(){
	var oldValue=parseInt($("#cellHangNum").val()); //取出现在的值，并使用parseInt转为int类型数据
    oldValue++;   //自加1
    $("#cellHangNum").val(oldValue);  
}
function cellLieSubNum(){
	var oldValue=parseInt($("#cellLieNum").val()); //取出现在的值，并使用parseInt转为int类型数据
	if(oldValue != 0){
		oldValue--;  //自减1
		$("#cellLieNum").val(oldValue);
	}
}
function cellLieAddNum(){
	var oldValue=parseInt($("#cellLieNum").val()); //取出现在的值，并使用parseInt转为int类型数据
    oldValue++;   //自加1
    $("#cellLieNum").val(oldValue);  
}

function saveCellConditions(){
	$("#but_save_cell").attr('disabled','true');
	var celltiaojianTable = $("#celltiaojianTable tr").length;
	if(celltiaojianTable < 2){
		alert("请先填写单元格条件");
		return false;
	}
	//获取单元格条件表的总行数
	var cellTableLength=$("#celltiaojianTable tr").length;
	//因为第一行是表头，所以从第二行开始
	for(var a = 1; a < cellTableLength; a++){
		var celltiaojianMapKey = $("#celltiaojianTable tr:eq("+ a +")").attr('id');
		//去掉TR id中的TR
		var celltiaojianMapKeyNew = celltiaojianMapKey.replace("Tr","");
		var cellShiTi = celltiaojianMap.get(celltiaojianMapKeyNew);
		cellShiTi.orderId = a;
	}
	var templateId = $("#templateId").val();
	var cellText=JSON.stringify(celltiaojianMap);
	var row = $("#cellHangNum").val();
	var col = $("#cellLieNum").val();
	
	$.ajax({
	   type: "POST",
	   url: ctx+"/reportTemplateConditionsController/saveCellConditions/"+templateId,
	   data: {cellText:cellText,row:row,col:col},
	   success: function(){
		   alert("保存成功");
		   $('#but_save_cell').removeAttr("disabled"); 
	   }
	}); 
}
//删除一行
function deleteCelltiaojianTableTr(obj){
	var id=$(obj).parent().attr('id');
	//删除表格里面的TR
	$('#'+id).parent().remove();
	//删除MAP里面的单元格对象条件
	celltiaojianMap.remove(id);
}

